package org.xxpay.apibase.exception;

import org.apache.commons.lang3.StringUtils;
import org.xxpay.apibase.constant.PayEnum;

/**
 * @author: dingzhiwei
 * @date: 17/12/7
 * @description:
 */
public class ApiException extends RuntimeException {

    private PayEnum payEnum;

    private String extraMsg;

    public ApiException(PayEnum payEnum) {
        this.payEnum = payEnum;
    }

    public ApiException(PayEnum payEnum, String extraMsg) {
        this.payEnum = payEnum;
        this.extraMsg = extraMsg;
    }

    public ApiException(String extraMsg) {
        this.extraMsg = extraMsg;
    }

    public static ApiException build(PayEnum payEnum) {
        return new ApiException(payEnum);
    }

    public static ApiException build(String extraMsg) {
        return new ApiException(extraMsg);
    }

    public PayEnum getPayEnum() {
        return payEnum;
    }

    public String getExtraMsg() {
        return extraMsg;
    }

    public String getErrMsg() {

        if(payEnum == null) return extraMsg;

        String errMsg = payEnum.getMessage();
        if(StringUtils.isNotBlank(extraMsg)) {
            errMsg += "[" + extraMsg + "]";
        }
        return errMsg;
    }

    @Override
    public String getMessage()
    {
        return getErrMsg();
    }

}
